import { Entity, PrimaryGeneratedColumn, Column, OneToMany,  } from "typeorm";
import { ManualTimestamp } from "../../../decorators/manual-timestamp.decorator";
import { CasinoChibiBet } from "./casino-chibi-bet.entity";

export enum ChibiRoundStatus {
  BETTING = 0, // 下注中
  DRAWING = 1, // 开奖中
  SETTLED = 2, // 已结算
}

export interface ChibiResultData {
  winningSide: number; // 1: 魏国, 2: 蜀国, 3: 吴国
  battleReport: string;
  sideStats?: Record<number, number>;
  reportStr?: string;
}

@Entity("casino_chibi_rounds")
@ManualTimestamp()
export class CasinoChibiRound {
  @PrimaryGeneratedColumn()
  id!: number;

  @Column({
    name: "start_time",
    type: "datetime",
    comment: "开始时间",
  })
  startTime!: Date;

  @Column({
    name: "end_time",
    type: "datetime",
    comment: "结束时间",
  })
  endTime!: Date;

  @Column({
    type: "tinyint",
    default: ChibiRoundStatus.BETTING,
    comment: "状态",
  })
  status!: ChibiRoundStatus;

  @Column({
    name: "winning_side",
    type: "tinyint",
    nullable: true,
    comment: "获胜国家：1-魏，2-蜀，3-吴",
  })
  winningSide!: number | null;

  @Column({
    name: "battle_report",
    type: "text",
    nullable: true,
    comment: "战斗报告",
  })
  battleReport!: string | null;

  @Column({
    name: "report_str",
    type: "text",
    nullable: true,
    comment: "报告字符串",
  })
  reportStr!: string | null;

  @Column({
    name: "wei_total_gold",
    type: "bigint",
    default: 0,
    comment: "魏国总下注金币",
  })
  weiTotalGold!: number;

  @Column({
    name: "wei_total_silver",
    type: "bigint",
    default: 0,
    comment: "魏国总下注银币",
  })
  weiTotalSilver!: number;

  @Column({
    name: "shu_total_gold",
    type: "bigint",
    default: 0,
    comment: "蜀国总下注金币",
  })
  shuTotalGold!: number;

  @Column({
    name: "shu_total_silver",
    type: "bigint",
    default: 0,
    comment: "蜀国总下注银币",
  })
  shuTotalSilver!: number;

  @Column({
    name: "wu_total_gold",
    type: "bigint",
    default: 0,
    comment: "吴国总下注金币",
  })
  wuTotalGold!: number;

  @Column({
    name: "wu_total_silver",
    type: "bigint",
    default: 0,
    comment: "吴国总下注银币",
  })
  wuTotalSilver!: number;

  @Column("datetime", { name: "created_at", comment: "创建时间" })
  created_at!: Date;

  @Column("datetime", { name: "updated_at", nullable: true, comment: "更新时间" })
  updated_at!: Date | null;

  // 关联关系 - 移除外键约束，只保留字段引用
  // // @OneToMany => CasinoChibiBet, (bet: CasinoChibiBet) => bet.round)
  // // chibiBets!: CasinoChibiBet[];

  // 计算属性
  get isActive(): boolean {
    return this.status === ChibiRoundStatus.BETTING;
  }

  get isDrawing(): boolean {
    return this.status === ChibiRoundStatus.DRAWING;
  }

  get isSettled(): boolean {
    return this.status === ChibiRoundStatus.SETTLED;
  }

  get remainingTime(): number {
    if (this.isSettled) return 0;
    const now = new Date();
    const end = new Date(this.endTime);
    return Math.max(0, Math.floor((end.getTime() - now.getTime()) / 1000));
  }

  get totalBetAmount(): number {
    return (
      this.weiTotalGold +
      this.weiTotalSilver +
      this.shuTotalGold +
      this.shuTotalSilver +
      this.wuTotalGold +
      this.wuTotalSilver
    );
  }

  // 类型安全的获取开奖结果方法
  getChibiResult(): ChibiResultData | null {
    if (!this.winningSide) {
      return null;
    }

    return {
      winningSide: this.winningSide,
      battleReport: this.battleReport || "",
      sideStats: {
        1: this.weiTotalGold + this.weiTotalSilver,
        2: this.shuTotalGold + this.shuTotalSilver,
        3: this.wuTotalGold + this.wuTotalSilver,
      },
      reportStr: this.reportStr || undefined,
    };
  }
}
